﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using System.Collections;
using System.Text.RegularExpressions;

namespace BYManageInfoSystem
{
    public partial class Frm_FeeZXZTruePayTab : DockContent
    {
        private bool m_bClearing = false;
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.SJPackingTab m_bllSJPackingTab = new DGBY.BLL.SJPackingTab();

   //     private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
        private DGBY.BLL.FeeZXZTruePayTab m_bllFeeZXZTruePayTab = new DGBY.BLL.FeeZXZTruePayTab();
        private bool m_bInsertFlag = false;
        private bool m_bCopyInsertFlag = false;
        private int m_nDelIndex = -1;
        private DGBY.BLL.DG_FeeLockTab m_bllDG_FeeLockTab = new DGBY.BLL.DG_FeeLockTab();
        private DGBY.BLL.DG_FeeTruePayLockTab m_bllDG_FeeTruePayLockTab = new DGBY.BLL.DG_FeeTruePayLockTab();
        private DGBY.Model.FeeZXZTruePayTab m_modelFeeZXZTruePayTab = new DGBY.Model.FeeZXZTruePayTab();
        private DGBY.BLL.HxdNoTab m_bllHxdNoTab = new DGBY.BLL.HxdNoTab();

        Hashtable m_SQLStringList = new Hashtable();
        ArrayList m_arrayList = new ArrayList();
        private string m_strBianHaoTemp = "";
        public Frm_FeeZXZTruePayTab()
        {
            InitializeComponent();
        }
        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllFeeZXZTruePayTab.GetList(strWhere);
            ultraGrid1.DataSource = null;

            DataSet dsTemp = m_bllFactoryDataTab.GetList("");

            this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFac.DisplayMember = "工厂简称";
            this.ultraDropDownFac.ValueMember = "FactoryDataTabID";
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["工厂简称"].Hidden = false;


            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["实付费用计提年月"].Hidden = true;

           // ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["工厂名称"].ValueList = this.ultraDropDownFac;

            ultraComboEditor1.DataSource = ds.Tables[0];
            ultraComboEditor1.DisplayMember = "收款人";
            ultraComboEditor1.ValueMember = "收款人";

            ultraComboEditor2.DataSource = ds.Tables[0];
            ultraComboEditor2.DisplayMember = "纸箱证公司";
            ultraComboEditor2.ValueMember = "纸箱证公司";
        }
        private void Frm_FeeZXZTruePayTab_Load(object sender, EventArgs e)
        {
            //SyncSBMonth();
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }
        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);

            //   UltraGridBand band = e.Layout.Bands[0];
            //   band.ColumnFilters["原币金额"].FilterConditions.Clear();
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.GreaterThan, 5);
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.LessThan, 10);
            //   band.ColumnFilters["原币金额"].LogicalOperator = FilterLogicalOperator.And;
        }
        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["编号是否手动生成"].Width = 30;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实付日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对公费用编号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对私费用编号"].Width = 130;
            ultraGrid1.DisplayLayout.Bands[0].Columns["是否直接开票"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["录入日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["开票日期"].Width = 95;
            ultraGrid1.DisplayLayout.Bands[0].Columns["发票号码"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["工厂名称"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["部门"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应付实付金额"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收金额"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对公公式"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["对私公式"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["纸箱证公司"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["经办人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["收款人"].Width = 60;
            ultraGrid1.DisplayLayout.Bands[0].Columns["应收费用计提年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["实付费用计提年月"].Width = 50;
            ultraGrid1.DisplayLayout.Bands[0].Columns["备注"].Width = 100;
        }
        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            InitColumnWidth();
            SummarySettings averageSummary =
e.Layout.Bands[0].Summaries.Add(
"应付实付金额",
SummaryType.Sum,
e.Layout.Bands[0].Columns["应付实付金额"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;

            averageSummary =
 e.Layout.Bands[0].Summaries.Add(
 "应收金额",
 SummaryType.Sum,
 e.Layout.Bands[0].Columns["应收金额"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;

            e.Layout.Bands[0].Columns["收款人"].EditorControl = this.ultraComboEditor1;
            e.Layout.Bands[0].Columns["纸箱证公司"].EditorControl = this.ultraComboEditor2;
            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            //ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            //ClassUltraGridCommonSet.LocalPrintPreveiw();
            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "部门" || str == "应付实付金额"
                    || str == "对公费用编号" || str == "对私费用编号"
                    || str == "录入日期" || str == "经办人" || str == "实付日期")
                {
                    column2.CellAppearance.BackColor = Color.Coral;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "工厂名称")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "工厂名称")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private void ResetRecord(int nFeeSJTruePayTabID)
        {

            try
            {
                DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTab
                    = m_bllFeeZXZTruePayTab.GetModel(nFeeSJTruePayTabID);


                modelFeeZXZTruePayTab.TruePayDate = null;


                //modelFeeZXZTruePayTab.KPDate = null;

                modelFeeZXZTruePayTab.InvoiceNo = "";
                modelFeeZXZTruePayTab.InputDate = null;
                modelFeeZXZTruePayTab.TruePayMonth = 0;
                modelFeeZXZTruePayTab.ShouldPayMonth = 0;
                //       modelFeeZXZTruePayTab.ShouldRecvNotRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应付实付金额"].Value.ToString()); ;

                modelFeeZXZTruePayTab.ZXZCompany = "";
                modelFeeZXZTruePayTab.ToPubFormula = "";
                modelFeeZXZTruePayTab.ToPrivateFormula = "";

                modelFeeZXZTruePayTab.ToPubBianHao = "";
                modelFeeZXZTruePayTab.ToPrivateBianHao = "";
                modelFeeZXZTruePayTab.JinBanPerson = "";
                modelFeeZXZTruePayTab.RecvMonyePerson = "";

                //modelFeeZXZTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeZXZTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeZXZTruePayTab.Note = "";



                modelFeeZXZTruePayTab.TruePayMoney = 0;




                modelFeeZXZTruePayTab.bitFiledReserv1 = false;
                modelFeeZXZTruePayTab.bitFiledReserv2 = false;

                modelFeeZXZTruePayTab.stringFiledReserv1 = "";
                modelFeeZXZTruePayTab.stringFiledReserv2 = "";
                modelFeeZXZTruePayTab.stringFiledReserv3 = "";
                modelFeeZXZTruePayTab.stringFiledReserv4 = "";

                modelFeeZXZTruePayTab.intFiledReserv1 = 0;
                modelFeeZXZTruePayTab.intFiledReserv2 = 0;
                modelFeeZXZTruePayTab.intFiledReserv3 = 0;
                modelFeeZXZTruePayTab.intFiledReserv4 = 0;
                // modelFeeZXZTruePayTab.intFiledReserv4 = 0;
                DateTime dtNow = System.DateTime.Now.Date;
                modelFeeZXZTruePayTab.datetimeFiledReserv1 = dtNow;
                modelFeeZXZTruePayTab.datetimeFiledReserv2 = dtNow;

                modelFeeZXZTruePayTab.decimalFiledReserv1 = 0;
                modelFeeZXZTruePayTab.decimalFiledReserv2 = 0;
                modelFeeZXZTruePayTab.decimalFiledReserv3 = 0;
                modelFeeZXZTruePayTab.decimalFiledReserv4 = 0;

                m_bllFeeZXZTruePayTab.Update(modelFeeZXZTruePayTab);

                Frm_Main.Log2DB(this.Text, "删除", modelFeeZXZTruePayTab.FeeZXZTruePayTabID.ToString());


            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }


        private void ResetUltraGridRow(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                CurrentUGR.Cells["对公费用编号"].Value = "";
                CurrentUGR.Cells["对私费用编号"].Value = "";

                CurrentUGR.Cells["实付日期"].Value = DBNull.Value;

                CurrentUGR.Cells["录入日期"].Value = DBNull.Value;
                CurrentUGR.Cells["开票日期"].Value = System.DateTime.Now.Date;

                CurrentUGR.Cells["实付费用计提年月"].Value = 0;
                CurrentUGR.Cells["应收费用计提年月"].Value = 0;
                CurrentUGR.Cells["应付实付金额"].Value = 0;
                CurrentUGR.Cells["应收金额"].Value = 0;
                CurrentUGR.Cells["对公公式"].Value = "";
                CurrentUGR.Cells["对私公式"].Value = "";
                CurrentUGR.Cells["经办人"].Value = "";
                CurrentUGR.Cells["收款人"].Value = "";
                CurrentUGR.Cells["纸箱证公司"].Value = "";
                CurrentUGR.Cells["发票号码"].Value = "";
                //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeZXZTruePayTab.IsToRecvPayTab;
                //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeZXZTruePayTab.IsToFinCheckMoneyTab;

                CurrentUGR.Cells["是否直接开票"].Value = false;
                CurrentUGR.Cells["备注"].Value = "";

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }


        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                if (m_bClearing)
                {
                    m_bClearing = false;
                    return false;
                }
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                int nFeeZXZTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTab
                    = m_bllFeeZXZTruePayTab.GetModel(nFeeZXZTruePayTabID);

                string strBGH = CurrentUGR.Cells["工厂名称"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelFeeZXZTruePayTab.FactoryDataTabID = Convert.ToInt32(CurrentUGR.Cells["工厂名称"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllFactoryDataTab.GetList("FactoryNameJX = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelFeeZXZTruePayTab.FactoryDataTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryDataTabID"].ToString(), 10);
                    }
                }
               // string strContractNo = CurrentUGR.Cells["合同编号"].Value.ToString();

                if (CurrentUGR.Cells["实付日期"].Text != "")
                {
                    DateTime dtTemp = Convert.ToDateTime(CurrentUGR.Cells["实付日期"].Value.ToString());
                    modelFeeZXZTruePayTab.TruePayDate = dtTemp.Date;
                }
                if (CurrentUGR.Cells["开票日期"].Text != "")
                {
                    DateTime dtTemp = Convert.ToDateTime(CurrentUGR.Cells["开票日期"].Value.ToString());
                    modelFeeZXZTruePayTab.KPDate = dtTemp.Date;
                }
                modelFeeZXZTruePayTab.InvoiceNo = CurrentUGR.Cells["发票号码"].Value.ToString();
                
                if (modelFeeZXZTruePayTab.ToPrivateBianHao == "" && modelFeeZXZTruePayTab.ToPubBianHao == "")
                    modelFeeZXZTruePayTab.InputDate = System.DateTime.Now.Date;

                modelFeeZXZTruePayTab.TruePayMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
                modelFeeZXZTruePayTab.ShouldPayMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
                //       modelFeeZXZTruePayTab.ShouldRecvNotRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应付实付金额"].Value.ToString()); ;
                modelFeeZXZTruePayTab.bitFiledReserv1 = (bool)CurrentUGR.Cells["是否直接开票"].Value;
                modelFeeZXZTruePayTab.ZXZCompany = CurrentUGR.Cells["纸箱证公司"].Value.ToString();
                modelFeeZXZTruePayTab.ToPubFormula = CurrentUGR.Cells["对公公式"].Value.ToString();
                modelFeeZXZTruePayTab.ToPrivateFormula = CurrentUGR.Cells["对私公式"].Value.ToString();
                if (CurrentUGR.Cells["对公公式"].Value.ToString() != ""
                    && CurrentUGR.Cells["对私公式"].Value.ToString() != "")
                {
                    MessageBox.Show("对公公式,对私公式不能同时添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                modelFeeZXZTruePayTab.JinBanPerson = modelFeeZXZTruePayTab.JinBanPerson != ""
                    ? CurrentUGR.Cells["经办人"].Value.ToString()
                    : Frm_Main.m_strLoginUser;
                modelFeeZXZTruePayTab.RecvMonyePerson = CurrentUGR.Cells["收款人"].Value.ToString();

                //modelFeeZXZTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeZXZTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeZXZTruePayTab.Note = CurrentUGR.Cells["备注"].Value.ToString();


                if (modelFeeZXZTruePayTab.ToPubFormula != "")
                    modelFeeZXZTruePayTab.TruePayMoney
                        = this.ultraCalcManager1.Calculate(modelFeeZXZTruePayTab.ToPubFormula).ToDecimal();
                else if (modelFeeZXZTruePayTab.ToPrivateFormula != "")
                    modelFeeZXZTruePayTab.TruePayMoney
                        = this.ultraCalcManager1.Calculate(modelFeeZXZTruePayTab.ToPrivateFormula).ToDecimal();
                else
                    modelFeeZXZTruePayTab.TruePayMoney = 0;

                CurrentUGR.Cells["应付实付金额"].Value = modelFeeZXZTruePayTab.TruePayMoney;
                CurrentUGR.Cells["收款人"].Value = modelFeeZXZTruePayTab.RecvMonyePerson;
                CurrentUGR.Cells["经办人"].Value = modelFeeZXZTruePayTab.JinBanPerson;
                CurrentUGR.Cells["录入日期"].Value = modelFeeZXZTruePayTab.InputDate;

                modelFeeZXZTruePayTab.ShouldRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应收金额"].Value);

                bRet = IsFeeTruePayLocked(CurrentUGR) && CheckFeeTruePayModify(modelFeeZXZTruePayTab);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                m_bllFeeZXZTruePayTab.Update(modelFeeZXZTruePayTab);
                Frm_Main.Log2DB(this.Text, "修改", modelFeeZXZTruePayTab.FeeZXZTruePayTabID.ToString());
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;

                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }
        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTab
                    = new DGBY.Model.FeeZXZTruePayTab();

                modelFeeZXZTruePayTab.FactoryDataTabID = 1;

                // string strContractNo = CurrentUGR.Cells["合同编号"].Value.ToString();

                if (CurrentUGR.Cells["实付日期"].Text != "")
                {
                    DateTime dtTemp = Convert.ToDateTime(CurrentUGR.Cells["实付日期"].Value.ToString());
                    modelFeeZXZTruePayTab.TruePayDate = dtTemp.Date;
                }
                if (CurrentUGR.Cells["开票日期"].Text != "")
                {
                    DateTime dtTemp = Convert.ToDateTime(CurrentUGR.Cells["开票日期"].Value.ToString());
                    modelFeeZXZTruePayTab.KPDate = dtTemp.Date;
                }

                DateTime dtNow = System.DateTime.Now.Date;
                modelFeeZXZTruePayTab.InvoiceNo = CurrentUGR.Cells["发票号码"].Value.ToString();
                modelFeeZXZTruePayTab.InputDate = dtNow;
                modelFeeZXZTruePayTab.TruePayMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
                modelFeeZXZTruePayTab.ShouldPayMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
                //       modelFeeZXZTruePayTab.ShouldRecvNotRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应付实付金额"].Value.ToString()); ;

                modelFeeZXZTruePayTab.ZXZCompany = CurrentUGR.Cells["纸箱证公司"].Value.ToString();
                modelFeeZXZTruePayTab.ToPubFormula = CurrentUGR.Cells["对公公式"].Value.ToString();
                modelFeeZXZTruePayTab.ToPrivateFormula = CurrentUGR.Cells["对私公式"].Value.ToString();
                if (CurrentUGR.Cells["对公公式"].Value.ToString() != ""
                    && CurrentUGR.Cells["对私公式"].Value.ToString() != "")
                {
                    MessageBox.Show("对公公式,对私公式不能同时添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }

                modelFeeZXZTruePayTab.JinBanPerson = modelFeeZXZTruePayTab.JinBanPerson != ""
                    ? CurrentUGR.Cells["经办人"].Value.ToString()
                    : Frm_Main.m_strLoginUser;
                modelFeeZXZTruePayTab.RecvMonyePerson = CurrentUGR.Cells["收款人"].Value.ToString();

                //modelFeeZXZTruePayTab.IsToRecvPayTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至应收应付登记表"].Value.ToString(), 10);
                //modelFeeZXZTruePayTab.IsToFinCheckMoneyTab = Convert.ToInt32(CurrentUGR.Cells["是否已提取至财务对帐"].Value.ToString(), 10);
                modelFeeZXZTruePayTab.Note = CurrentUGR.Cells["备注"].Value.ToString();


                if (modelFeeZXZTruePayTab.ToPubFormula != "")
                    modelFeeZXZTruePayTab.TruePayMoney
                        = this.ultraCalcManager1.Calculate(modelFeeZXZTruePayTab.ToPubFormula).ToDecimal();
                else if (modelFeeZXZTruePayTab.ToPrivateFormula != "")
                    modelFeeZXZTruePayTab.TruePayMoney
                        = this.ultraCalcManager1.Calculate(modelFeeZXZTruePayTab.ToPrivateFormula).ToDecimal();
                else
                    modelFeeZXZTruePayTab.TruePayMoney = 0;

                CurrentUGR.Cells["应付实付金额"].Value = modelFeeZXZTruePayTab.TruePayMoney;
                CurrentUGR.Cells["收款人"].Value = modelFeeZXZTruePayTab.RecvMonyePerson;
                CurrentUGR.Cells["经办人"].Value = modelFeeZXZTruePayTab.JinBanPerson;
                CurrentUGR.Cells["录入日期"].Value = modelFeeZXZTruePayTab.InputDate;



                modelFeeZXZTruePayTab.bitFiledReserv1 = false;
                modelFeeZXZTruePayTab.bitFiledReserv2 = false;

                modelFeeZXZTruePayTab.stringFiledReserv1 = "";
                modelFeeZXZTruePayTab.stringFiledReserv2 = "";
                modelFeeZXZTruePayTab.stringFiledReserv3 = "";
                modelFeeZXZTruePayTab.stringFiledReserv4 = "";

                modelFeeZXZTruePayTab.intFiledReserv1 = 0;
                modelFeeZXZTruePayTab.intFiledReserv2 = 0;
                modelFeeZXZTruePayTab.intFiledReserv3 = 0;
                modelFeeZXZTruePayTab.intFiledReserv4 = 0;
                // modelFeeZXZTruePayTab.intFiledReserv4 = 0;
                modelFeeZXZTruePayTab.datetimeFiledReserv1 = dtNow;
                modelFeeZXZTruePayTab.datetimeFiledReserv2 = dtNow;

                modelFeeZXZTruePayTab.decimalFiledReserv1 = 0;
                modelFeeZXZTruePayTab.decimalFiledReserv2 = 0;
                modelFeeZXZTruePayTab.decimalFiledReserv3 = 0;
                modelFeeZXZTruePayTab.decimalFiledReserv4 = 0;

                int nID = m_bllFeeZXZTruePayTab.Add(modelFeeZXZTruePayTab);
                Frm_Main.Log2DB(this.Text, "增加", nID.ToString());
                //ShowAllRecord("");
                ShowAllRecord("FeeZXZTruePayTab.FeeZXZTruePayTabID = '" + nID.ToString() + "'");
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private bool AddCopyGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTab = new DGBY.Model.FeeZXZTruePayTab();
                modelFeeZXZTruePayTab = m_modelFeeZXZTruePayTab;
                modelFeeZXZTruePayTab.ToPrivateBianHao = "";
                modelFeeZXZTruePayTab.ToPubBianHao = "";
                modelFeeZXZTruePayTab.TruePayDate = null;
                int nID = m_bllFeeZXZTruePayTab.Add(modelFeeZXZTruePayTab);

                Frm_Main.Log2DB(this.Text, "复制增加", nID.ToString());
                //ShowAllRecord("");
                ShowAllRecord("FeeZXZTruePayTab.FeeZXZTruePayTabID = '" + nID.ToString() + "'");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["工厂名称"].Value = 1;
            CurrentUGR.Cells["对公费用编号"].Value = "";
            CurrentUGR.Cells["对私费用编号"].Value = "";

            DateTime dtNow = System.DateTime.Now.Date;
            CurrentUGR.Cells["实付日期"].Value = dtNow;

            CurrentUGR.Cells["录入日期"].Value = dtNow;
            CurrentUGR.Cells["开票日期"].Value = dtNow;

            CurrentUGR.Cells["实付费用计提年月"].Value = 0;
            CurrentUGR.Cells["应收费用计提年月"].Value = 0;
            CurrentUGR.Cells["应付实付金额"].Value = 0;

            CurrentUGR.Cells["对公公式"].Value = "";
            CurrentUGR.Cells["对私公式"].Value = "";
            CurrentUGR.Cells["经办人"].Value = "";
            CurrentUGR.Cells["收款人"].Value = "";
            CurrentUGR.Cells["纸箱证公司"].Value = "";
            CurrentUGR.Cells["发票号码"].Value = "";
            //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeZXZTruePayTab.IsToRecvPayTab;
            //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeZXZTruePayTab.IsToFinCheckMoneyTab;


            CurrentUGR.Cells["备注"].Value = "";

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }
        }
        private void InitNewAddRowValueCopyContract(int nIndex)
        {

            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["工厂名称"].Value = m_modelFeeZXZTruePayTab.FactoryDataTabID;
            CurrentUGR.Cells["对公费用编号"].Value = m_modelFeeZXZTruePayTab.ToPubBianHao;
            CurrentUGR.Cells["对私费用编号"].Value = m_modelFeeZXZTruePayTab.ToPrivateBianHao;
            if (m_modelFeeZXZTruePayTab.TruePayDate != null)
                CurrentUGR.Cells["实付日期"].Value = m_modelFeeZXZTruePayTab.TruePayDate;
            if (m_modelFeeZXZTruePayTab.InputDate != null)
                CurrentUGR.Cells["录入日期"].Value = m_modelFeeZXZTruePayTab.InputDate;

            CurrentUGR.Cells["实付费用计提年月"].Value = m_modelFeeZXZTruePayTab.TruePayMonth;
            CurrentUGR.Cells["应收费用计提年月"].Value = m_modelFeeZXZTruePayTab.ShouldPayMonth;
            CurrentUGR.Cells["应付实付金额"].Value = m_modelFeeZXZTruePayTab.TruePayMoney;

            CurrentUGR.Cells["对公公式"].Value = m_modelFeeZXZTruePayTab.ToPubFormula;
            CurrentUGR.Cells["对私公式"].Value = m_modelFeeZXZTruePayTab.ToPrivateFormula;
            CurrentUGR.Cells["经办人"].Value = m_modelFeeZXZTruePayTab.JinBanPerson;
            CurrentUGR.Cells["收款人"].Value = m_modelFeeZXZTruePayTab.RecvMonyePerson;
            CurrentUGR.Cells["纸箱证公司"].Value = m_modelFeeZXZTruePayTab.ZXZCompany;

            //CurrentUGR.Cells["是否已提取至应收应付登记表"].Value = m_modelFeeZXZTruePayTab.IsToRecvPayTab;
            //CurrentUGR.Cells["是否已提取至财务对帐"].Value = m_modelFeeZXZTruePayTab.IsToFinCheckMoneyTab;


            CurrentUGR.Cells["备注"].Value = m_modelFeeZXZTruePayTab.Note;

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddCopyGoodsDetail(nIndex);

            }

        }

        private void 复制增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!m_bInsertFlag)
                {
                    UltraGridRow CurrentUGRFrom;
                    CurrentUGRFrom = ultraGrid1.Rows[m_nDelIndex];
                    int nSelectFeeZXZTruePayTabID = Convert.ToInt32(CurrentUGRFrom.Cells[0].Value.ToString(), 10);
                    m_modelFeeZXZTruePayTab = m_bllFeeZXZTruePayTab.GetModel(nSelectFeeZXZTruePayTabID);
                    m_modelFeeZXZTruePayTab.InputDate = System.DateTime.Now.Date;
                    m_bCopyInsertFlag = true;
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();

                }
            }
            catch
            {
                MessageBox.Show("没有选择记录，无法复制！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;

        }

        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
           // InitNewAddRowValueCopyContract(e.Row.Index);
            m_bInsertFlag = true;
            if (m_bCopyInsertFlag == false)
                InitNewAddRowValue(e.Row.Index);
            else
            {

                m_bCopyInsertFlag = false;
                InitNewAddRowValueCopyContract(e.Row.Index);

            }

        }
        private bool CheckModify(UltraGridRow CurrentUGR)
        {
            int nYearMonth = Convert.ToInt32(CurrentUGR.Cells["应收费用计提年月"].Value.ToString(), 10);
            DataSet ds = m_bllDG_FeeLockTab.GetList("FeeName = '纸箱证' and IsLock = 1 and LockYearMonth = '" + nYearMonth.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return false;
            }
            return true;
        }
        private bool IsFeeTruePayLocked(UltraGridRow CurrentUGR)
        {
            int nYearMonth = Convert.ToInt32(CurrentUGR.Cells["实付费用计提年月"].Value.ToString(), 10);
            DataSet ds = m_bllDG_FeeTruePayLockTab.GetList("FeeName = '纸箱证' and IsLock = 1 and LockYearMonth = '" + nYearMonth.ToString() + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        private bool CheckFeeTruePayModify(DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTab)
        {
            DGBY.Model.FeeZXZTruePayTab oldModelFeeZXZTruePayTab = m_bllFeeZXZTruePayTab.GetModel(modelFeeZXZTruePayTab.FeeZXZTruePayTabID);
            if (null != oldModelFeeZXZTruePayTab)
            {
                //限制修改实付日期、费用编号、是否直接开票、应付实付金额
                if (oldModelFeeZXZTruePayTab.TruePayDate != modelFeeZXZTruePayTab.TruePayDate
                    || oldModelFeeZXZTruePayTab.bitFiledReserv1 != modelFeeZXZTruePayTab.bitFiledReserv1
                    || oldModelFeeZXZTruePayTab.TruePayMoney != modelFeeZXZTruePayTab.TruePayMoney
                    || oldModelFeeZXZTruePayTab.ToPubBianHao != modelFeeZXZTruePayTab.ToPubBianHao
                    || oldModelFeeZXZTruePayTab.ToPrivateBianHao != modelFeeZXZTruePayTab.ToPrivateBianHao)
                    return true;
            }

            return false;
        }
        private void 删除本行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                bRet = IsFeeTruePayLocked(CurrentUGR);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (ultraGrid1.ActiveRow != null)
                {
                    //UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
      
                    ultraGrid1.ActiveRow.Delete(false);
 
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectFeeZXZTruePayTabID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTabOld
                    = m_bllFeeZXZTruePayTab.GetModel(nSelectFeeZXZTruePayTabID);

                if (modelFeeZXZTruePayTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllFeeZXZTruePayTab.Delete(nSelectFeeZXZTruePayTabID);

                        Frm_Main.Log2DB(this.Text, "删除", modelFeeZXZTruePayTabOld.FeeZXZTruePayTabID.ToString());

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void SyncSBMonth()
        {
            DataSet dsTemp = m_bllHxdNoTab.ShowZhDjbRec("year(HxdNoTab.SBDate) > 1900");
            DataSet ds = m_bllFeeZXZTruePayTab.GetList("ShouldPayMonth = 0 or TruePayMonth = 0");
            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
            {
                string strContractNo = ds.Tables[0].Rows[j]["合同编号"].ToString();
                int nFeeZXZTruePayTabID = (int)ds.Tables[0].Rows[j]["FeeZXZTruePayTabID"];
                DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTabOld
               = m_bllFeeZXZTruePayTab.GetModel(nFeeZXZTruePayTabID);

                DataRow[] Rows = dsTemp.Tables[0].Select("合同编号 = '" + strContractNo.ToString() + "'");
                if (Rows.Length > 0)
                {
                    DateTime dtSBMonth = (DateTime)Rows[0]["申报日期"];
                    int nSBMonth = dtSBMonth.Month;
                    modelFeeZXZTruePayTabOld.ShouldPayMonth = nSBMonth;
                    modelFeeZXZTruePayTabOld.TruePayMonth = nSBMonth;
                    m_bllFeeZXZTruePayTab.Update(modelFeeZXZTruePayTabOld);
                }

            }
            //ShowAllRecord("");
            //  MessageBox.Show("同步申报月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private string MakeZXZBianHao(string strDepart, string strPuborPri, string strDate)
        {

            string strRet = "ZX" + ClassUtility.m_dicCn2EngDep[strDepart]
                + "N" + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;
            if (m_strBianHaoTemp != "")
            {
                strRet += m_strBianHaoTemp;
            }
            else
            {
                DateTime dtTruePay = this.dateTimePicker1.Value.Date;
                DataSet dsTemp;
                //if (strPuborPri == "公")
                //    dsTemp = m_bllFeeSJTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + @"' and ToPubBianHao like '%" + strRet + "%'");
                //else

                dsTemp = m_bllFeeZXZTruePayTab.GetMaxList("TruePayDate = '" + dtTruePay + "'");


                if (dsTemp.Tables[0].Rows.Count > 0)
                {
                    string strTest = "";

                    string strTestPub = dsTemp.Tables[0].Rows[0]["对公费用编号"].ToString();

                    string strTestPri = dsTemp.Tables[0].Rows[0]["对私费用编号"].ToString();

                    if (strTestPri.CompareTo(strTestPub) >= 0)
                        strTest = strTestPri;
                    else
                        strTest = strTestPub;


                    string strTemp = strTest.Remove(0, 8);
                    if (strTemp != "")
                    {
                        int nTemp = Convert.ToInt32(strTemp, 10) + 1;
                        strTemp = nTemp.ToString().PadLeft(3, '0'); ;
                    }
                    else
                    {
                        strTemp = "001";
                    }

                    strRet += strTemp;
                    m_strBianHaoTemp = strTemp;
                }
                else
                {
                    strRet = strRet + "001";
                    m_strBianHaoTemp = "001";
                }
            }

            return strRet;
        }
        private string MakeZXZBianHaoWithoutLiuShuiNo(string strDepart
            , string strPuborPri, string strDate)
        {
            string strRet = "ZX" + ClassUtility.m_dicCn2EngDep[strDepart]
    + "N" + ClassUtility.m_dicCn2EngGS[strPuborPri] + strDate;

            //string strRet = "ZX" + strDepart + strComp + strPuborPri + strDate;
            return strRet;
        }

        private void buttonBatchModifyDate_Click(object sender, EventArgs e)
        {
            m_strBianHaoTemp = "";
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的实付日期么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                for (int i = 0; i < ugr.Length; i++)
                {
                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTabOld
                                        = m_bllFeeZXZTruePayTab.GetModel(nID);

                    DateTime? dtTruePayOld = modelFeeZXZTruePayTabOld.TruePayDate;


                    modelFeeZXZTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                    if (dtTruePayOld == null
&& (modelFeeZXZTruePayTabOld.ToPrivateFormula != "" || modelFeeZXZTruePayTabOld.ToPubFormula != ""))
                    {
                        if (modelFeeZXZTruePayTabOld.ToPrivateFormula != "")
                        {
                            string strBianHao = MakeZXZBianHao(ugr[i].Cells["部门"].Value.ToString()
                           , "私"
                            , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect);

                            modelFeeZXZTruePayTabOld.ToPrivateBianHao = strRet;
                            CurrentUGR.Cells["对私费用编号"].Value = strRet;
                        }
                        if (modelFeeZXZTruePayTabOld.ToPubFormula != "")
                        {
                            string strBianHao = MakeZXZBianHao(ugr[i].Cells["部门"].Value.ToString()
                           , "公"
                            , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd"));

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect);

                            modelFeeZXZTruePayTabOld.ToPubBianHao = strRet;
                            CurrentUGR.Cells["对公费用编号"].Value = strRet;
                        }

                        modelFeeZXZTruePayTabOld.bitFiledReserv2 = false;//编号是否手动生成

                        CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                        CurrentUGR.Cells["编号是否手动生成"].Value = false;

                        m_bllFeeZXZTruePayTab.TransUpdate(modelFeeZXZTruePayTabOld
                            , m_SQLStringList, m_arrayList, i);

                        //CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                    }
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改实付日期", this.dateTimePicker1.Value.Date.ToString());
                MessageBox.Show("批量修改实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void buttonExportExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonSetEmpty_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                bool bCanModify = true;

                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                DialogResult dr = MessageBox.Show("确实要批量置空以下" + ugr.Length.ToString() + "条记录的实付日期么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                List<string> lstDT = new List<string>();
                for (int i = 0; i < ugr.Length; i++)
                {
                    string strTemp = ugr[i].Cells["对公费用编号"].Value.ToString() + ugr[i].Cells["对私费用编号"].Value.ToString();

                    if (strTemp != "" && lstDT.Count == 0)
                        lstDT.Add(strTemp);
                    else if (lstDT.Count > 0 && !lstDT.Contains(strTemp))
                    {
                        bCanModify = false;
                        break;
                    }

                }

                if (!bCanModify)
                {
                    MessageBox.Show("费用编号不相同，不能批量置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();
                for (int i = 0; i < ugr.Length; i++)
                {
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];
                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTabOld
                                        = m_bllFeeZXZTruePayTab.GetModel(nID);
                    modelFeeZXZTruePayTabOld.TruePayDate = null;
                    modelFeeZXZTruePayTabOld.ToPrivateBianHao = "";
                    modelFeeZXZTruePayTabOld.ToPubBianHao = "";

                    m_bllFeeZXZTruePayTab.TransUpdate(modelFeeZXZTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

                    CurrentUGR.Cells["对私费用编号"].Value = "";
                    CurrentUGR.Cells["对公费用编号"].Value = "";
                    CurrentUGR.Cells["实付日期"].Value = DBNull.Value;
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量置空实付日期", "");
                MessageBox.Show("批量置空实付日期完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }

        private void 增加一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                //  row.Cells[2] = DateTime.Parse("1900-01-01");
            }
        }

        private void ultraGrid1_AfterCellUpdate(object sender, CellEventArgs e)
        {
            if (e.Cell.Column.Key == "工厂名称")
            {
                int nIndex = e.Cell.Row.Index;
                if (nIndex == -1)
                    return;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                string strID = e.Cell.Value.ToString();


                DataSet ds = m_bllFactoryDataTab.GetList("FactoryDataTabID = '" + strID + "'");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {

                    CurrentUGR.Cells["部门"].Value = ds.Tables[0].Rows[0]["所属部门"].ToString();
                   

                }

            }
            else if (e.Cell.Column.Key == "对公公式"
                || e.Cell.Column.Key == "对私公式")
            {

                int nIndex = e.Cell.Row.Index;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];
                if (e.Cell.Value.ToString() == "")
                    return;
                Decimal dJinE = Convert.ToDecimal(e.Cell.Value.ToString());
                if (dJinE > 0)
                {
                    CurrentUGR.Cells["应收金额"].Value = dJinE;
                }
            }
        }

        private void buttonModYearMon_Click(object sender, EventArgs e)
        {
            if (!ClassInputValid.IsNum(textBoxYearMonth.Text))
            {
                errorProvider1.SetError(textBoxYearMonth, "必须输入数字");
                return;
            }
            int nYearMon = Convert.ToInt32(textBoxYearMonth.Text, 10);
            if (nYearMon < 201001 || nYearMon > 219912)
            {
                errorProvider1.SetError(textBoxYearMonth, "必须正确的年月，形如201206");
                return;
            }

            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的应收费用计提年月么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;


                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTabOld
                                        = m_bllFeeZXZTruePayTab.GetModel(nID);
                    modelFeeZXZTruePayTabOld.ShouldPayMonth = nYearMon;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    m_bllFeeZXZTruePayTab.TransUpdate(modelFeeZXZTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

                    CurrentUGR.Cells["应收费用计提年月"].Value = nYearMon.ToString();
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改计提年月", nYearMon.ToString());
                MessageBox.Show("批量修改应收费用计提年月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void 清空该条记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确实要清空该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nIndex];

                bool bRet = CheckModify(CurrentUGR);
                if (!bRet)
                {
                    MessageBox.Show("费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                bRet = IsFeeTruePayLocked(CurrentUGR);
                if (bRet)
                {
                    MessageBox.Show("实付费用已锁定，不能修改或删除记录！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ResetUltraGridRow(nIndex);

                int nFeeSJTruePayTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                ResetRecord(nFeeSJTruePayTabID);
                m_bClearing = true;
            }
            else
            {
                MessageBox.Show("请选中要清空的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量置空以下" + ugr.Length.ToString() + "条记录的应收费用计提年月么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;


                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTabOld
                                        = m_bllFeeZXZTruePayTab.GetModel(nID);
                    modelFeeZXZTruePayTabOld.ShouldPayMonth = 0;
                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    m_bllFeeZXZTruePayTab.TransUpdate(modelFeeZXZTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);

                    CurrentUGR.Cells["应收费用计提年月"].Value = "0";
                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量置空计提年月", "");
                MessageBox.Show("批量修改应收费用计提年月完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private bool IsLiuShuiNo(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[0-9]{3}$");
        }
        private bool IsLiuShuiNo(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return false;
            }
            else if (!IsLiuShuiNo(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须001-999之间的流水号");
                return false;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return true;
            }
        }
        private void buttonBatchModFeeNo_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {
                if (!IsLiuShuiNo(this.errorProvider1, this.textBoxLiuShuiNo))
                    return;
                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的费用编号么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTabOld
                                        = m_bllFeeZXZTruePayTab.GetModel(nID);

                    DateTime? dtTruePayOld = modelFeeZXZTruePayTabOld.TruePayDate;
                    if (dtTruePayOld != null)
                    {
                        MessageBox.Show("原记录实付日期不为空，不能修改，若要修改，请先置空实付日期!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    modelFeeZXZTruePayTabOld.TruePayDate = this.dateTimePicker1.Value.Date;

                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];

                    if (modelFeeZXZTruePayTabOld.ToPrivateFormula != "" || modelFeeZXZTruePayTabOld.ToPubFormula != "")
                    {
                        if (modelFeeZXZTruePayTabOld.ToPrivateFormula != "")
                        {
                            string strBianHao = MakeZXZBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                  , "私"
                   , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                   );

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                            modelFeeZXZTruePayTabOld.ToPrivateBianHao = strRet;
                            CurrentUGR.Cells["对私费用编号"].Value = strRet;
                        }

                        if (modelFeeZXZTruePayTabOld.ToPubFormula != "")
                        {
                            string strBianHao = MakeZXZBianHaoWithoutLiuShuiNo(ugr[0].Cells["部门"].Value.ToString()
                   , "公"
                   , this.dateTimePicker1.Value.Date.ToString("yyyyMMdd")
                   );

                            string strIsFacDirect = (bool)ugr[i].Cells["是否直接开票"].Value ? "G" : "B";
                            string strRet = strBianHao.Insert(7, strIsFacDirect) + this.textBoxLiuShuiNo.Text;

                            modelFeeZXZTruePayTabOld.ToPubBianHao = strRet;
                            CurrentUGR.Cells["对公费用编号"].Value = strRet;
                        }

                        modelFeeZXZTruePayTabOld.bitFiledReserv2 = true;//编号是否手动生成
                        m_bllFeeZXZTruePayTab.TransUpdate(modelFeeZXZTruePayTabOld
                            , m_SQLStringList, m_arrayList, i);

                        CurrentUGR.Cells["实付日期"].Value = this.dateTimePicker1.Value.Date;
                        CurrentUGR.Cells["编号是否手动生成"].Value = true;
                    }


                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改费用编号", this.textBoxLiuShuiNo.Text);
                MessageBox.Show("批量修改费用编号完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void buttonModRecvPerson_Click(object sender, EventArgs e)
        {
            m_SQLStringList.Clear();
            m_arrayList.Clear();

            try
            {

                Infragistics.Win.UltraWinGrid.UltraGridRow[] ugr = ultraGrid1.Rows.GetFilteredInNonGroupByRows();

                DialogResult dr = MessageBox.Show("确实要批量修改以下" + ugr.Length.ToString() + "条记录的收款人么", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.Cancel)
                    return;

                for (int i = 0; i < ugr.Length; i++)
                {

                    int nID = Convert.ToInt32(ugr[i].Cells[0].Value.ToString(), 10);
                    DGBY.Model.FeeZXZTruePayTab modelFeeZXZTruePayTabOld
                                        = m_bllFeeZXZTruePayTab.GetModel(nID);





                    UltraGridRow CurrentUGR = ultraGrid1.Rows[ugr[i].Index];


                    CurrentUGR.Cells["收款人"].Value = this.textBoxRecvPerson.Text;

                    modelFeeZXZTruePayTabOld.RecvMonyePerson = this.textBoxRecvPerson.Text;//编号是否手动生成
                    m_bllFeeZXZTruePayTab.TransUpdate(modelFeeZXZTruePayTabOld
                        , m_SQLStringList, m_arrayList, i);




                }

                m_bllSJPackingTab.ExecuteSqlTran(m_SQLStringList, m_arrayList);
                Frm_Main.Log2DB(this.Text, "批量修改收款人", this.textBoxRecvPerson.Text);
                MessageBox.Show("批量修改收款人完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
    }
}
